package com.im.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.im.app.authConfig.GoogleAuthenticator;

import com.im.app.model.Test;
import com.im.app.service.ITestService;
import com.im.app.service.IUserService;

import com.im.app.utils.*;
import com.im.app.utils.uuid.IdUtils;

import org.jim.core.ImPacket;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.Command;
import org.jim.core.packets.MsgType;
import org.jim.core.packets.RespBody;
import org.jim.server.JimServerAPI;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping(value = "/test")
public class TestController {

    @RequestMapping("/sendMessage")
    public ResponseResult<Object> index(){
        String userId = "0844a6d2-4c77-4de8-8c50-cd53e4b36465";
        ChatBody chatBody = ChatBody.newBuilder().build();
        chatBody.setId(IdUtils.simpleUUID());
        chatBody.setFrom("system");
        chatBody.setTo(userId);
        chatBody.setMsgType(MsgType.MSG_TYPE_TEXT.getNumber());
        chatBody.setContent("hello");
        chatBody.setCreateTime(System.currentTimeMillis());
        chatBody.setCmd(11);
        ImPacket chatPacket = new ImPacket(Command.COMMAND_CHAT_REQ,new RespBody(Command.COMMAND_CHAT_REQ,chatBody).toByte());
        boolean b = JimServerAPI.sendToUser(userId, chatPacket);
        if (b){
            return Response.makeOKRsp();
        }else {
            return Response.makeErrRsp("发送失败");
        }
    }

    @GetMapping("/test")
    public ResponseResult<Object> test(){
        return Response.makeOKRsp().setData(1);
    }

    @GetMapping("/getIP")
    public ResponseResult<Object> getIP(String ip) throws Exception {
        String ipAddr = IpToAddressUtil.getRealAddressByIP(ip);
        return Response.makeOKRsp(ipAddr);
    }

    @GetMapping("/getSecretKey")
    public ResponseResult<Object> getSecretKey(HttpServletRequest request) throws Exception {
      String secretKey = GoogleAuthenticator.getSecretKey();

      return Response.makeOKRsp(secretKey);
    }

    @GetMapping("/verify")
    public ResponseResult<Object> verify(String code,String secret){
        boolean c = GoogleAuthenticator.checkCode(secret, Long.parseLong(code), System.currentTimeMillis());
        if (c){
           return Response.makeOKRsp();
        }
        return Response.makeErrRsp("验证失败");
    }

    public final static String CHECKURL="https://wx.rrbay.com/pro/wxUrlCheck2.ashx?key=50bb79a297a19ca082212894bcdf3044&url=";

    @GetMapping("/checkUrl")
    public ResponseResult<Object> checkUrl(String url){
        String content = HttpUtils.sendGet(CHECKURL+url);
        JSONObject object=(JSONObject) JSONObject.parse(content);
        String code = object.getString("Code");
        String msg = object.getString("Msg");
        ResponseResult<Object> result = new ResponseResult<Object>();
        result.setMsg(msg);
        result.setCode(Integer.parseInt(code));
        return result;
    }


    @GetMapping("/logout")
    public ResponseResult<Object> logout(String userId){
        IUserService bean = SpringUtils.getBean(IUserService.class);
        com.im.app.model.User userByUserId = bean.getUserByUserId(userId);
        if (userByUserId != null){
            userByUserId.setLoginOutTime(new Date());
            bean.updateById(userByUserId);
            return Response.makeOKRsp();
        }
        return Response.makeErrRsp("1");
    }

}
